package com.yeu.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.yeu.model.Event3;

public class ExcelAction extends ActionSupport implements ModelDriven<Event3> {

	private static final long serialVersionUID = 6671156922189748649L;

	// 用户Ajax返回数据
	private String result;
	// struts的属性驱动模式，自动填充页面的属性到这里
	private String information;

	// 下载  事件
	private Event3 event3 = new Event3();

	public String print() throws WriteException, IOException { // 打印出excel 表格,提供客户端下载
		ActionContext actionContext = ActionContext.getContext();
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
		Map<String, Object> session = actionContext.getSession();// 获取session
		if(session.get("Info")!=null){
		event3.setFileStream(createExcel(session.get("Info").toString()));
		event3.setFileFileName("110YEU"+df.format(new Date())+".xls");
		event3.setFileContentType("application/vnd.ms-excel");
		session.remove("Info");
		return INPUT;
		}else{
		this.result=" 请 重新 尝试打印".toString();
		return SUCCESS;
		}
	}

	public String saveinfo() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.remove("Info");
		session.put("Info", information);// 将页面 返回数据存入session
		return SUCCESS;
	}

	public ByteArrayInputStream createExcel(String info) throws IOException // 打印成execl 格式的文件
, WriteException
	{

		// 固定数据
		String[] TotalItem = new String[] { "报案日期", "报案人姓名", "学生联系电话", "案件简述", "案件号" };
		String[] DataItem = new String[] { "EVENTDATE", "REPORTERNAME","CONTACTPHONE", "EVENTCONTENT", "EVENTID" };
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		WritableWorkbook workbook = Workbook.createWorkbook(bos);// 创建工作薄

		WritableSheet sheet = workbook.createSheet("Info1", 0);// 创建新的一页

		WritableCellFormat titleFormate = new WritableCellFormat();// 生成一个单元格样式控制对象
		WritableFont bold = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD);// 设置字体种类和黑体显示,字体为Arial,字号大小为10,采用黑体显示
		titleFormate.setFont(bold);// 设置字体
		titleFormate.setAlignment(jxl.format.Alignment.CENTRE);// 单元格中的内容水平方向居中
		titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中
		sheet.setColumnView(0, 15);
		sheet.setColumnView(1, 12);
		sheet.setColumnView(2, 15);
		sheet.setColumnView(3, 60);
		sheet.setColumnView(4, 30);
		
        sheet.setRowView(0, 350);// 设置第一行的高

		for (int i = 0; i < TotalItem.length; i++) {// 打印出第一行标题
				Label title = new Label(i, 0, TotalItem[i], titleFormate);
				sheet.addCell(title);
			}
        
		// 创建要显示的具体内容 大
		WritableCellFormat ContentFormat = new WritableCellFormat();
		ContentFormat.setAlignment(jxl.format.Alignment.CENTRE);// 单元格中的内容水平方向居中
		ContentFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 单元格的内容垂直方向居中

		// 刷数据1 Begin
		int sum1 = 0;
		Map<String, String> map = new HashMap<String, String>();
		JSONObject json = JSONObject.fromObject(info);
		Iterator<?> keys = json.keys();
		while (keys.hasNext()) {
		sum1++;
		String key = (String) keys.next();
		String value = json.get(key).toString();
		map.put(key, value);
		}
		// 刷数据1 End

		// 打印出内容
		for (int i = 0; i < sum1; i++) {
		Map<String, String> Tempmap = new HashMap<String, String>();
		JSONObject jsonout = JSONObject.fromObject(map.get(String.valueOf(i)));
		Iterator<?> keysout = jsonout.keys();
		while (keysout.hasNext()) {
			String key = (String) keysout.next();
			String value = jsonout.get(key).toString();
			Tempmap.put(key, value);
			}
		
		for (int j = 0; j < TotalItem.length; j++) {
			Label formate = new Label(j, i + 1, Tempmap.get(DataItem[j].toString()), ContentFormat);
			sheet.addCell(formate);
				}
			}

			// 把创建的内容写入到输出流中，并关闭输出流
		workbook.write();
		workbook.close();
		return convertOutToIn(bos);
	}

	private static ByteArrayInputStream convertOutToIn(ByteArrayOutputStream bos) {
		ByteArrayInputStream inputStream = null;
		try {
			byte[] bts = bos.toByteArray();
			inputStream = new ByteArrayInputStream(bts);
		} catch (ImageFormatException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getInformation() {
		return information;
	}

	public void setInformation(String information) {
		this.information = information;
	}

	@Override
	public Event3 getModel() {
		return this.event3;
	}

}
